package com.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.service.ITestService;

import lombok.Data;

@RestController
public class FileUploadController {

	@Autowired
	private ITestService testService;

	@PostMapping("/api/his/upload")
	public String uploadFile(@RequestParam MultipartFile zipFile, @RequestParam MultipartFile imageFile)
			throws Exception {
		System.out.println(zipFile.getOriginalFilename());
		System.out.println(imageFile.getOriginalFilename());
		return testService.saveFile(zipFile);
	}

	@GetMapping("/api/image")
	public Map<String, String> image() throws IOException {
		Map<String, String> map = new HashMap<>();
		map.put("name", "zhangsike");
		map.put("image", testService.getBase64Image("C:\\Users\\ZC20181120-1\\Desktop\\image.jpg", "jpeg"));
		return map;
	}

	@GetMapping("/init")
	public String init() {
		return UUID.randomUUID().toString();
	}

	@GetMapping("/api/his/models")
	public Map<String, Object> list() {
		List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		File file = new File("D:/storage/");
		File[] files = file.listFiles();
		for (File f : files) {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("name", f.getName());
			map.put("image", "/img/1.png");
			list.add(map);
		}
		Map<String, Object> map = new HashMap<>();
		map.put("code", 200);
		map.put("msg", "");
		map.put("entity", list);
		return map;
	}

	@GetMapping("/api/his/delete/{name}")
	public String remove(@PathVariable String name) {
		File file = new File("D:/storage/" + name);
		if (file.isFile()) {
			return file.delete() ? responseStr : responseErrStr;
		}

		return responseErrStr;
	}

	@GetMapping("/api/his/start/{name}")
	public String start(@PathVariable String name) {
		map.put("state", "running");
		map.put("name", name);
		return responseStr;
	}

	static String responseStr = "{\"code\":200}";
	static String responseErrStr = "{\"code\":400,\"msg\":\"操作失败\"}";
	static Map<String, Object> rootMap = new HashMap<>();
	static Map<String, Object> map = new HashMap<>();
	static {
		map.put("name", "love cat"); // 模型名
		map.put("state", "running");// 模型状态free/pause/running
		map.put("currentLayer", 24); // 当前层
		map.put("totalLayer", 200); // 总层数
		map.put("curLayerRemTime", 30); // 当前层固化剩余时间
		map.put("printPercent", 56); // 打印进度
		map.put("toalTime", "05:20:25");// 总时间
		map.put("pastTime", "02:20:25");// 已用时间
		map.put("remainingTime", "03:20:25"); // 剩余时间
		map.put("threadHander", null);
		map.put("image",
				"");

		rootMap.put("code", 200);
		rootMap.put("msg", "");
		rootMap.put("entity", map);
	}

	@GetMapping("/api/mon/info")
	public Map<String, Object> info() {
		if ("running".equals(map.get("state"))) {
			int precent = (int) map.get("printPercent");
			precent++;
			if (precent > 100) {
				precent = 0;
			}

			int curLayerRemTime = (int) map.get("curLayerRemTime");
			curLayerRemTime--;
			if (curLayerRemTime < 0) {
				curLayerRemTime = 60;
			}
			map.put("printPercent", precent);
			map.put("curLayerRemTime", curLayerRemTime);
		}
		return rootMap;
	}

	@GetMapping("/api/mon/start")
	public String start() {
		map.put("state", "running");
		return responseStr;
	}

	@GetMapping("/api/mon/pause")
	public String pause() {
		map.put("state", "pause");
		return responseStr;
	}

	@GetMapping("/api/mon/stop")
	public String stop() {
		map.put("state", "free");
		map.put("currentLayer", 0); // 当前层
		map.put("totalLayer", 0); // 总层数
		map.put("curLayerRemTime", 0); // 当前层固化剩余时间
		map.put("curLayerLastPercent", 0);// 当前剩固化剩余时间
		map.put("printPercent", 0); // 打印进度
		map.put("toalTime", "00:00:00");// 总时间
		map.put("pastTime", "00:00:00");// 已用时间
		map.put("remainingTime", "00:00:00"); // 剩余时间
		return responseStr;
	}
	
	@PostMapping("/post/user")
    public Map<String, String>  postUser(@RequestBody Map<String, String> map) {
        return map;
    }
	
	@PostMapping(value = "/post/form")
    public Map<String, String>  postForm(@RequestParam Map<String, String> map) {
        return map;
    }

}
